<template>
	<view class="authorization">
		<view class="log-box">
			<h3>数字酷</h3>
			<!-- <p>随时掌控《国标》体质情况。</p> -->
		</view>
		<p v-if="shouquanTishi" class="shouquanTishi">{{ shouquanMsg }}</p>
		<view class="btn btn1"><button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信登录</button></view>
		<view class="btn cacel" @tap="cacel"><button>取消</button></view>
		<view class="serve flexCenter">
			<jiuai-checkbox value="cb" @change="changeBox" checked="true" fontSize="22" checkBoxSize="24" />
			我已阅读并同意
			<navigator url="/pages/privacyPolicy/privacyPolicy">服务隐私声明</navigator>
		</view>
	</view>
</template>

<script>
// import {request} from '@/common/request.js';
import { getLogin } from '@/api/login.js';
import { trim } from '@/utils/util.js';
import { setParams, clear } from '@/utils/storageSync.js';
import jiuaiCheckbox from '@/components/jiuai-checkbox/jiuai-checkbox';
export default {
	data() {
		return {
			// request,
			checked: true,
			SessionKey: '',
			OpenId: '',
			nickName: null,
			avatarUrl: null,
			wxlogin_code: '',
			getPhoneNumberStatus: false,
			shouquanTishi: false,
			jsonData: {},
			shouquanMsg: '请完成手机号授权以继续使用'
		};
	},
	components: {
		jiuaiCheckbox
	},
	onLoad() {
		//默认加载
		this.wxlogin();
	},
	onShow() {
		uni.hideHomeButton(); //取消头部小房子
		this.wxlogin();
	},
	methods: {
		wxlogin() {
			let _this = this;
			uni.getSetting({
				withSubscriptions: true,
				success(res) {
					if (res.authSetting['scope.userInfo'] === true) {
					}
				}
			});
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					_this.wxlogin_code = loginRes.code; // 获取code
				}
			});
		},
		// 微信用户信息授权
		getUserInfo() {
			this.getPhoneNumberStatus = true;
			// var _this = this;
			// if (_this.checked) {
			// 	uni.getUserProfile({
			// 		desc: '用户昵称显示',
			// 		success: function(res) {
			// 			const encryptedData = res.encryptedData;
			// 			const iv = res.iv;
			// 			const userInfo = res.userInfo;
			// 			_this.nickName = userInfo.nickName; //昵称
			// 			const avatarUrl = userInfo.avatarUrl; //头像
			// 			const gender = userInfo.gender; //性别 0：未知、1：男、2：女
			// 			const province = userInfo.province;
			// 			const city = userInfo.city;
			// 			const country = userInfo.country;

			// 			setParams('nickName', _this.nickName);
			// 			setParams('avatarUrl', avatarUrl);
			// 			_this.getPhoneNumberStatus = true;
			// 			_this.shouquanTishi = true;
			// 		},
			// 		fail: function(res) {
			// 			console.log('fail========', res);
			// 		}
			// 	});
			// } else if (!_this.checked) {
			// 	uni.showModal({
			// 		content: '请先勾选服务条款说明',
			// 		showCancel: false
			// 	});
			// }
		},
		// 微信手机号授权
		getPhoneNumber(e) {
			var _this = this;

			if (_this.checked) {
				if (e.detail.errMsg === 'getPhoneNumber:fail user deny') {
					//拒绝授权
					uni.showToast({
						title: '授权失败',
						duration: 2000,
						icon:'none'
					});
				} else if (e.detail.errMsg === 'getPhoneNumber:ok') {
					//授权成功
					uni.showLoading({
						title: '登录中...'
					});
					let jsonData = {
						code: _this.wxlogin_code,
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv,
						nickname: _this.nickName || '微信用户'
					};

					// 正式登陆逻辑
					getLogin(jsonData).then(res => {
						let { data, success, errMsg } = res.data;

						if (success) {
							let openId = data.openId;
							let token = data.token;
							setParams('token', token, '604800'); //存token到本地，时间7天
							let payload = {
								isLogin: uni.getStorageSync('token') ? true : false,
								openId: openId
							};

							uni.showToast({
								title: '登录成功',
								duration: 2000
							});
							uni.hideLoading(); //隐藏loading

							// 登陆成功
							uni.reLaunch({
								url: '/pages/activity/activity'
							});
						} else {
							uni.hideLoading();

							uni.showToast({
								title: '登录失败',
								duration: 2000
							});
						}
					});
				}
			} else if (!_this.checked) {
				uni.showModal({
					content: '请先勾选服务条款说明',
					showCancel: false
				});
			}
		},

		changeBox(e) {
			//选中切换事件
			this.checked = e.detail.checked;
		},

		cacel() {
			clear();
			uni.reLaunch({
				url: '/pages/activity/activity'
			});
		}
	}
};
</script>

<style></style>
